const { Console } = require('console')
const { logLevel } = require('../base-config')

class Logger {
    constructor() {
        this.level = !logLevel ? 1 : ['debug', 'info', 'warn', 'error', 'off'].indexOf(logLevel)
        this.console = new Console({ stdout: process.stdout, stderr: process.stderr })
        this.format = require('util').format
    }
    setLevel(levelName) {
        this.level = ['debug', 'info', 'warn', 'error', 'off'].indexOf(levelName)
    }
    debug(message, ...optionParams) {
        if (this.level <= 0)
            this.console.log(message, ...optionParams)
    }
    info(message, ...optionParams) {
        if (this.level <= 1)
            this.console.log(message, ...optionParams)
    }
    warn(message, ...optionParams) {
        if (this.level <= 2)
            this.console.error(message, ...optionParams)
    }
    error(message, ...optionParams) {
        if (this.level <= 3)
            this.console.error(message, ...optionParams)
    }
    log(message, ...optionParams) {
        if (this.level < 4)
            this.console.log(message, ...optionParams)
    }
}

module.exports = new Logger()